package com.booter.mvel;

import org.mvel2.UnresolveablePropertyException;
import org.mvel2.integration.VariableResolver;
import org.mvel2.integration.VariableResolverFactory;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.IntStream;

/**
 * @author lcw
 * @date 2022/8/1 10:49
 */
public abstract class CustomBaseVariableResolverFactory implements VariableResolverFactory {

    private static final long serialVersionUID = 929279430938294935L;
    protected Map<String, VariableResolver> variableResolvers = new ConcurrentHashMap<>();
    protected VariableResolverFactory nextFactory;

    protected int indexOffset;//int indexOffset = 0
    protected String[] indexedVariableNames;
    protected VariableResolver[] indexedVariableResolvers;

    private boolean tiltFlag;

    @Override
    public VariableResolverFactory getNextFactory() {
        return nextFactory;
    }

    @Override
    public VariableResolverFactory setNextFactory(VariableResolverFactory resolverFactory) {
        return nextFactory = resolverFactory;
    }

    @Override
    public VariableResolver getVariableResolver(String name) {
        if (isResolveable(name)) {
            if (variableResolvers.containsKey(name)) {
                return variableResolvers.get(name);
            } else if (nextFactory != null) {
                return nextFactory.getVariableResolver(name);
            }
        }

        throw new UnresolveablePropertyException("unable to resolve variable '" + name + "'");
    }

    public boolean isNextResolveAble(String name) {//NOPMD
        return nextFactory != null && nextFactory.isResolveable(name);
    }

    public void appendFactory(VariableResolverFactory resolverFactory) {
        if (nextFactory == null) {
            nextFactory = resolverFactory;
        } else {
            VariableResolverFactory vrf = nextFactory;
            while (vrf.getNextFactory() != null) {
                vrf = vrf.getNextFactory();
            }
            vrf.setNextFactory(nextFactory);
        }
    }

    public void insertFactory(VariableResolverFactory resolverFactory) {
        if (nextFactory == null) {
            nextFactory = resolverFactory;
        } else {
            resolverFactory.setNextFactory(nextFactory = resolverFactory);
        }
    }


    @Override
    public Set<String> getKnownVariables() {
        if (nextFactory == null) {
            return new HashSet<>(variableResolvers.keySet());
            //   return new HashSet<>(0);
        } else {
            HashSet<String> vars = new HashSet<>(variableResolvers.keySet());
            vars.addAll(nextFactory.getKnownVariables());
            return vars;
        }
    }

    @Override
    public VariableResolver createIndexedVariable(int index, String name, Object value) {
        if (nextFactory != null) {
            return nextFactory.createIndexedVariable(index - indexOffset, name, value);
        } else {
            throw new RuntimeException("cannot create indexed variable: " + name + "(" + index + "). operation not supported by resolver: " + this.getClass().getName());
        }
    }

    @Override
    public VariableResolver getIndexedVariableResolver(int index) {
        if (nextFactory != null) {
            return nextFactory.getIndexedVariableResolver(index - indexOffset);
        } else {
            throw new RuntimeException("cannot access indexed variable: " + index + ".  operation not supported by resolver: " + this.getClass().getName());
        }
    }

    @Override
    public VariableResolver createIndexedVariable(int index, String name, Object value, Class<?> type) {
        if (nextFactory != null) {
            return nextFactory.createIndexedVariable(index - indexOffset, name, value, type);
        } else {
            throw new RuntimeException("cannot access indexed variable: " + name + "(" + index + ").  operation not supported by resolver.: " + this.getClass().getName());
        }
    }

    public Map<String, VariableResolver> getVariableResolvers() {
        return variableResolvers;
    }

    public void setVariableResolvers(Map<String, VariableResolver> variableResolvers) {
        this.variableResolvers = variableResolvers;
    }

    public String[] getIndexedVariableNames() {
        return indexedVariableNames;
    }

    public void setIndexedVariableNames(String[] indexedVariableNames) {
        this.indexedVariableNames = indexedVariableNames;
    }

    @Override
    public int variableIndexOf(String name) {
        if (indexedVariableNames != null) {
            return IntStream.range(0, indexedVariableNames.length).filter(i -> name.equals(indexedVariableNames[i])).findFirst().orElse(-1);
        }
        return -1;
    }

    @Override
    public VariableResolver setIndexedVariableResolver(int index, VariableResolver resolver) {
        if (indexedVariableResolvers == null) {
            return (indexedVariableResolvers = new VariableResolver[indexedVariableNames.length])[index - indexOffset] = resolver;
        } else {
            return indexedVariableResolvers[index - indexOffset] = resolver;
        }
    }

    @Override
    public boolean isIndexedFactory() {
        return false;
    }

    @Override
    public boolean tiltFlag() {
        return tiltFlag;
    }

    @Override
    public void setTiltFlag(boolean tiltFlag) {
        this.tiltFlag = tiltFlag;
        if (nextFactory != null) {
            nextFactory.setTiltFlag(tiltFlag);
        }
    }
}
